package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.ServletUtils;
import com.spzx.common.core.utils.poi.ExcelUtil;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.log.enums.BusinessType;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.domain.Category;
import com.spzx.product.service.CategoryService;
import com.spzx.system.api.domain.SysUser;
import com.spzx.system.api.model.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-10-26
 */
@Tag(name ="分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;


    @Operation(summary = "获取分类树")
    @GetMapping("/treeSelect/{parentId}")
    //@PathVariable将url上的模板变量绑定到方法的形参是上
    public AjaxResult getCategoryTree(@PathVariable Long parentId){
        //调用categoryService中的父ID获取分类数据
       List<Category> categoryList= categoryService.getCategoryTree(parentId);
       return AjaxResult.success(categoryList);
    }


    @Operation(summary = "将所有的分类数据导出到excel文件中")
    @PostMapping("/export")
    public void exportCategoryTreeToExcel(HttpServletResponse response , Category category ){

        //获取所有的分类数据
        List<Category> categoryList = categoryService.list();
        //创建了ExcelUtil对象
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.exportExcel(response, categoryList, "分类数据");
    }

    @Operation(summary = "导入分类数据时下载excel模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        util.importTemplateExcel(response, "分类数据");
    }

    @Operation(summary = "将Excel中的数据导入到数据库中")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        //创建ExcelUtil
        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
        //导入Excel文件
        List<Category> categoryList = util.importExcel(file.getInputStream());
        //获取登录的用户名
        String operName = SecurityUtils.getUsername();
        //导入Excel数据
        String message = categoryService.importCategory(categoryList, updateSupport, operName);
        return AjaxResult.success(message);
    }



    @InnerAuth
    @Operation(summary = "微服务内部远程调用获取1级分类的接口")
    @GetMapping(value = "/getFirstLeveCategory/{parentId}")
    public R<List<CategoryVo> > getFirstLeveCategory(@PathVariable Long parentId) {
        //调用当前类中的方法
        AjaxResult categoryTree = getCategoryTree(parentId);
        List<Category> categoryList = (List<Category>) categoryTree.get("data");
        //使用stream流将List<Category>转换为List<CategoryVo>
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            //复制属性
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());

        return R.ok(categoryVoList);
    }

    @InnerAuth
    @Operation(summary = "微服务内部远程调用获取分类嵌套列表的接口")
    @GetMapping(value = "/getCategorytreeList")
    public R<List<CategoryVo> > getCategoryTreeList() {
        List<CategoryVo> categoryVoList=  categoryService.getCategoryTreeList();
        return R.ok(categoryVoList);
    }

}
